package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CmnProductionDetailActivity;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 有机商品适配器
 */
public class OrganicGoodsAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Blog> mDatas;

    public OrganicGoodsAdapter(Context context, ArrayList<Blog> goods) {
        super(context);
        this.mContext = context;
        this.mDatas = goods;
    }

    @Override
    public int getCount() {
        return mDatas.size() == 0 ? 1 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_organic, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_unit)
        TextView tvUnit;
        int position = 0;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(mContext, CmnProductionDetailActivity.class);
                    it.putExtra("blog_id", mDatas.get(position).getId());
                    mContext.startActivity(it);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Blog blog = mDatas.get(position);
            ImageUtils.loadRadiusImageMid(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getName());
            tvPrice.setText("¥"+ uuUtils.formatAfterDot2(blog.getPrice()));
            tvUnit.setText("/"+blog.getUnit());
        }
    }
}
